package com.fesine.ch02.t3;

/**
 * 脏读数据测试
 * 在set方法上加了同步锁，get方法没有加会出现脏读数据
 * 在get上加入同步锁即可解决
 * @Author: Fesine
 * @CreateDate:2016/6/28
 * @UpdateUser:修改人名称
 * @UpdateDate:2016/6/28
 * @UpdateRemark:修改具体的内容
 */
public class PublicVar {

    private String username = "A";
    private String password = "AA";

    synchronized public void setValue(String username, String password) {
        try {
            this.username = username;
            Thread.sleep(5000);
            this.password = password;
            System.out.println("setValue method thread name ="+ Thread.currentThread().getName()
            +" username="+username+" password="+password);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public void getValue() {
        System.out.println("getValue method thread name =" + Thread.currentThread().getName()
                + " username=" + username + " password=" + password);
    }
}
